package com.vanysoft.framework.kaptcha;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author qi_ma
 */
@Configuration
@EnableConfigurationProperties(value = KaptchaProperties.class)
public class KaptchaConfiguration {
    public final KaptchaProperties properties;

    public KaptchaConfiguration(KaptchaProperties properties) {
        this.properties = properties;
    }

    @Bean
    public DefaultKaptcha kaptcha(Config kaptchaConfig) {
        DefaultKaptcha kaptcha = new DefaultKaptcha();
        kaptcha.setConfig(kaptchaConfig);
        return kaptcha;
    }

    @Bean
    public Config kaptchaConfig() {
        return new Config(properties.getProperties());
    }

    @Bean
    public ServletRegistrationBean kaptchaServletRegistrationBean(DefaultKaptcha kaptcha) {
        return new ServletRegistrationBean(new KaptchaServlet(kaptcha), "/code.jpg");
    }

}
